﻿using Newtonsoft.Json.Linq;
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace GBT.Tool.Core
{
    public class WXOCRAPI
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        static HttpRequest hr = new HttpRequest();

        public JObject getOCR_BusinessLicense(Stream img)
        {
            JObject joResult = new JObject();
            try
            {
                string url = "https://api.weixin.qq.com/cv/ocr/bizlicense?access_token=" + WXServiceAPI.AccessToken;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                byte[] buffer = new byte[img.Length];
                img.Read(buffer, 0, buffer.Length);
                img.Seek(0, SeekOrigin.Begin);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = buffer.Length;
                Stream requestStream = req.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);
                requestStream.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
                string backstr = sr.ReadToEnd();
                joResult = JObject.Parse(backstr);
                sr.Close();
                res.Close();
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                Console.Write(ex);
            }
            return joResult;
        }
    }
}
